/**
 * @author ming
 * @date 2021/8/7
 */
package org.lq.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement //自动管理事务
//扫描mapper文件夹
@MapperScan("org.lq.mapper")
@Configuration //配置类
public class MyBatisPlusConfig {
    //注册乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return  new OptimisticLockerInterceptor();
    }
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor  = new PaginationInterceptor();
        //设置请求的页面大于最大页后操作，true 回到首页 false 继续请求 ，默认false
        paginationInterceptor.setOverflow(false);
        //设置最大单页限制数量，默认500条 -1 不受限制
        //开启count 的join 优化只针对部分 left join
        return paginationInterceptor;
    }
    @Bean
    public ISqlInjector sqlInjector(){
        return new LogicSqlInjector();
    }
    @Bean
    @Profile({"test","dev"})
    public  PerformanceInterceptor performanceInterceptor(){
        PerformanceInterceptor pfi = new PerformanceInterceptor();
        pfi.setFormat(true);//开启sql格式化
        pfi.setMaxTime(1000);//设置最大的sql执行时间
        return pfi;
    }


}
